/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: general_uds_io.h
 *
 * Purpose: immplementation communication io API with unix domain socket(uds)
 *
 * Developer:
 *   wen.gu , 2023-06-29
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __UDSSIPC_GENERAL_UDS_IO_H__
#define __UDSSIPC_GENERAL_UDS_IO_H__

#include <string_view>

#include "core/com_io.h"
#include "core/thread.h"
#include "core/message_queue.h"


class GeneralUdsIo: public ComIo {
public:
    /**max_queue_size set as 0, inidicat unlimite the queue size */
    GeneralUdsIo(std::string_view io_name, uint32_t max_sender_queue_size = 0, uint32_t max_receiver_queue_size = 0);
    GeneralUdsIo(std::string_view io_name, MessageQueue::MessageQueuePtr send_list, MessageQueue::MessageQueuePtr recv_list);
    GeneralUdsIo(std::string_view io_name, MessageQueue::MessageQueuePtr recv_list, uint32_t max_sender_queue_size = 0);
    virtual ~GeneralUdsIo();

public: /** current class added API */
    void set_sender_priority(int32_t priority); /** the priority value see Thread */
    void set_receiver_priority(int32_t priority); /** the priority value see Thread */
public:
    virtual ComErrC clear(); // clear(drop) all data cached message in commmunication io(include sender and receiver).
    virtual ComErrC start(); // start sender and receiver to working
    virtual ComErrC stop(); //stop will call clear() also
public: /** sender API */

    virtual ComErrC send(ComMessagePtr msg_ptr);
    virtual ComErrC send(ComMessageType type, uint8_t* payload_data, uint32_t payload_size);

    /** this API will alloc message with 'payload_size' and call 'fill_handler' method to fill payload to message */
    virtual ComErrC send(ComMessageType type, uint32_t payload_size, std::function<ComErrC(ComMessagePtr)> fill_handler);
    virtual ComErrC flush(uint32_t timeout_ms); //wait for all message in sender to be sent.

public: /** receiver API */
    /** timeout_ms: see also 'class Timeout',
     * NoWait: if haven't message received, then return immediately
     * WaitForever: if haven't message received, then recv() will be block until receive a message or the ComIo is stopped
     * > 0: if haven't message received, then recv() will be block until receive a message or the ComIo is stopped or timeout
    */
    virtual ComMessagePtr recv(int32_t timeout_ms = Timeout::WaitForever); 

protected:
    void sender_worker();
    void receiver_worker();

protected:
    //status, true: connected, false, disconnected
    void report_status(bool status);
private:
    bool is_running_ = true;
    bool is_connected = true;
    MessageQueue::MessageQueuePtr sender_list_ = nullptr;
    MessageQueue::MessageQueuePtr receiver_list_ = nullptr;
    Thread* sender_worker_ = nullptr;
    Thread* receiver_worker_ = nullptr;
    std::string com_io_name_;
};


#endif /** !__UDSSIPC_GENERAL_UDS_IO_H__ */